﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.BLL;
using AD.Model;

public partial class Manager_User_PageData_ManangerUser : WebManagerForm
{
    public ManagerUserMod ManagerUser = new ManagerUserMod();
    public string OperTitle = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        if (!string.IsNullOrEmpty(action))
        {
            if (action.Equals("add"))
            {
                OperTitle = "增加管理用户";
                if (Page.IsPostBack)
                {
                    string userName = Request.Params["userName"];
                    string pass = Request.Params["pass"];
                    string pass2 = Request.Params["pass2"];
                    if (string.IsNullOrEmpty(userName))
                    {
                        Alert("用户名不能为空！");
                        return;
                    }
                    if (string.IsNullOrEmpty(pass))
                    {
                        Alert("密码不能为空！");
                        return;
                    }
                    if (!pass.Equals(pass2))
                    {
                        Alert("两次密码输入不一致！");
                        return;
                    }
                    if (ManagerUserBLL.CheckUser(userName))
                    {
                        int effect = ManagerUserBLL.Insert(new ManagerUserMod() { UserName = userName, Passwd = pass, IsValid = true });
                        if (effect > 0)
                        {
                            Alert("管理用户添加成功!");
                            RedirectNewURL("ManagerUserList.aspx");
                        }
                        else
                        {
                            Alert("管理用户添加失败!");
                        }
                    }
                    else
                    {
                        Alert("管理用户【" + userName + "】已经存在!");
                    }
                }
            }
            else if (action.Equals("edit")) {
                OperTitle = "修改管理用户";
                ManagerUserMod  tmp =null;
                int id = 0;
                int.TryParse(Request.QueryString["id"], out id);
                tmp = ManagerUserBLL.GetUserByIdToMod(id);
                if (Page.IsPostBack)
                {
                    string pass = Request.Params["pass"];
                    string pass2 = Request.Params["pass2"];
                    
                    if (string.IsNullOrEmpty(pass))
                    {
                        Alert("密码不能为空！");
                        return;
                    }
                    if (!pass.Equals(pass2))
                    {
                        Alert("两次密码输入不一致！");
                        return;
                    }
                    tmp.Passwd = pass;
                    int effect = ManagerUserBLL.Update(tmp);
                    if (effect > 0)
                    {
                        AlertRedirectBlock("管理用户修改成功!", "ManagerUserList.aspx?action=gopage&pageIndex=1");
                    }
                    else
                    {
                        Alert("管理用户修改失败!");
                    }
                }
                if (tmp != null)
                    ManagerUser = tmp;
            }
        }
    }
}